import http from "@/api";
import { AuthButtons, Login } from "@/api/interface/index";
import DynamicRouter from "@/assets/json/dynamicRouter.json";
import { encrypt } from "@/utils/encrypt";
import moment from "moment";
import qs from "qs";
export const refreshToken = (params?: any) => {
	return http.post<any>(`/user/token/refresh`, params);
};
/**
 * @name 登录模块
 */
// * 用户登录
export const loginApi = (params: Login.ReqLoginForm) => {
	let timestamp = moment().valueOf();
	let key = "aes" + timestamp;
	return http.post<Login.ResLogin>(
		`/auth/login`,
		{ ...params, password: encrypt(params.password, key, key) },
		{
			headers: { noLoading: true, "Content-Type": "application/x-www-form-urlencoded", authtimestamp: timestamp }
		}
	);

	// 正常 post json 请求  ==>  application/json
	return http.post<Login.ResLogin>(`/login`, params, { headers: { noLoading: true } }); // 控制当前请求不显示 loading
	return http.post<Login.ResLogin>(`/login`, {}, { params }); // post 请求携带 query 参数  ==>  ?username=admin&password=123456
	return http.post<Login.ResLogin>(`/login`, qs.stringify(params)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
	return http.get<Login.ResLogin>(`/login?${qs.stringify(params, { arrayFormat: "repeat" })}`); // 如果是 get 请求可以携带数组等复杂参数
};

// * 获取按钮权限
export const getAuthButtonListApi = (params: AuthButtons.ReqAuthButtons) => {
	return http.get<AuthButtons.ResAuthButtons>(`/menu/find`, params, { headers: { noLoading: true } });
	// 如果想让按钮权限变为本地数据，注释上一行代码，并引入本地 authButtons.json 数据
	// return AuthButtons;
};

// * 获取用户菜单列表
export const getAuthMenuListApi = async (username: string) => {
	// 如果想让菜单变为本地数据，注释上一行代码，并引入本地 dynamicRouter.json 数据
	let res = await http.get<Menu.MenuOptionsRows>(
		`/menu/find`,
		{ type: "menu", username: username },
		{ headers: { noLoading: true } }
	);
	if (import.meta.env.DEV) {
		let debugMenu = [
			{
				path: "/demos",
				name: "demos",
				component: "Layout",
				menuName: "调试菜单",
				children: DynamicRouter.result.records.children
			}
		];
		res.result.records.children = [...res.result.records.children, ...debugMenu];
	}
	return res;

	// 框架基础组件演示菜单
	return DynamicRouter;
};

// * 用户退出登录
export const logoutApi = () => {
	// return http.post(`/user/logout`);
};
